package xmlib;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.xml.serialize.OutputFormat;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

import javax.xml.parsers.*;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.*;
public class xmlhtelp {

	/**
	   * 添加图片之后，根据上传的类别，修改临时XML文件
	   *
	   * @param catalog
	   *          ,例如：01
	   * @param imgSrc
	   *          ,例如：book\0000000\040\a.jpg
	   * @param xmlPath
	   *          ,例如：D:\TEMP.XML
	   */
	  public static void addNodeToTempXML(String catalog, String imgSrc, String reportName,String xmlPath) {
	    Document document = load(xmlPath);
	    Element root = document.getDocumentElement();
	    if (root.hasChildNodes()) {
	      NodeList ids = root.getChildNodes();
	      for (int i = 0; i < ids.getLength(); i++) {
	        Node id = ids.item(i);
	        if (id.getNodeType() == Node.ELEMENT_NODE) {
	          String name = id.getAttributes().getNamedItem("name").getNodeValue();
	          if (name.equals(catalog)) {
	            Element page_mode = document.createElement("page");
	            page_mode.setAttribute("reportName", reportName);
	            Text textseg = document.createTextNode(imgSrc);
	            page_mode.appendChild(textseg);
	            id.appendChild(page_mode);
	            break;
	          }
	        }
	      }
	    }
	    doc2XmlFile(document, xmlPath);
	  }
	 
	  /**
	   * 删除图片之后，根据上传的类别，修改临时XML文件
	   *
	   * @param manual_catalog
	   * @param imgSrc
	   *          ,例如：book\0000000\040\a.jpg
	   * @param xmlPath
	   *          ,例如D:\TEMP.XML
	   */
	  public static void deleteNodeFromTempXML(String manual_catalog, String imgSrc, String xmlPath) {
	    Document document = load(xmlPath);
	    Node root = document.getDocumentElement();
	    if (root.hasChildNodes()) {
	      NodeList catalogs = root.getChildNodes();
	      for (int i = 0; i < catalogs.getLength(); i++) {
	        Node catalog = catalogs.item(i);
	        if (catalog.getNodeType() == Node.ELEMENT_NODE) {
	          String name = catalog.getAttributes().getNamedItem("name").getNodeValue();
	          if (name.equals(manual_catalog) && catalog.hasChildNodes()) {
	            NodeList pages = catalog.getChildNodes();
	            for (int j = 0; j < pages.getLength(); j++) {
	              Node page = pages.item(j);
	              if (page.getNodeType() == Node.ELEMENT_NODE) {
	                String src = page.getFirstChild().getNodeValue();
	                if (imgSrc.equals(src)) {
	                  catalog.removeChild(page);
	                  break;
	                }
	              }
	            }
	          }
	        }
	      }
	    }
	    doc2XmlFile(document, xmlPath);
	  }
	public static boolean doc2XmlFile(Document document, String filename) {
	    boolean flag = true;
	    try {
	      TransformerFactory tf=TransformerFactory.newInstance();   
	      Transformer transformer=tf.newTransformer();   
	      DOMSource source=new DOMSource(document);   
	      transformer.setOutputProperty(OutputFormat.ENCODING,"GB2312");   
	      transformer.setOutputProperty(OutputKeys.INDENT,"yes");   
	      PrintWriter pw=new PrintWriter(new FileOutputStream(filename));   
	      StreamResult result=new StreamResult(pw);   
	      transformer.transform(source,result); 
	    } catch (Exception ex) {
	      flag = false;
	      ex.printStackTrace();
	    }
	    return flag;
	  }
	 
	  public static Document load(String filename) {
	    Document document = null;
	    try {
	      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	      DocumentBuilder builder = factory.newDocumentBuilder();
	      document = builder.parse(new File(filename));
	      document.normalize();
	    } catch (Exception ex) {
	      ex.printStackTrace();
	    }
	    return document;
	  }
}
